package top.codedance.iotp.common.util.db;

import top.codedance.iotp.common.util.db.mysql.MysqlJDBCUtil;
import top.codedance.iotp.common.util.db.sqlite.SQLiteJDBCUtils;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.util.Properties;

public class JdbcUtilAdapter {
    public enum DB_TYPE {
        SQLITE, MYSQL
    }

    private JdbcUtil jdbcUtil;
    private DB_TYPE dbType;

    public JdbcUtilAdapter(Class<?> clazz, Logger logger, Properties properties){
        this.dbType = DB_TYPE.SQLITE;
        if (properties.containsKey("dbtype")) {
            if(properties.getProperty("dbtype").equals("mysql")){
                this.dbType = DB_TYPE.MYSQL;
            }
        }
        switch (this.dbType){
            case MYSQL:
                jdbcUtil = new MysqlJDBCUtil(clazz, logger, properties);
                break;
            case SQLITE:
            default:
                jdbcUtil = new SQLiteJDBCUtils(clazz, logger, properties);
                break;
        }
    }

    public Connection getConnection(){
        return jdbcUtil.getConnection();
    }

    public synchronized void returnConnection(Connection c){
        jdbcUtil.returnConnection(c);
    }

    public void destory(){
        jdbcUtil.destory();
    }

    public DB_TYPE getDbType() {
        return dbType;
    }
}
